<?php
namespace app\usercp\controller;

use app\usercp\exception\UsercpException;
use app\usercp\service\rbac\Users\Service;
use app\usercp\model\ManageUser;
use think\facade\Cache;
use think\facade\Env;
use think\facade\Session;
use think\Paginator;
use think\paginator\driver\Bootstrap;
use think\Request;
use think\response\Json;

class Index extends Usercp
{
    public function index()
    {
        $site_info = model('custom/custom_setting', 'service')->get($this->custom['current_id']);
        $this->assign('site_info',$site_info);
        return $this->fetch('index');
    }

    public function home(){
        // 我的客户站点统计
        $site_count = [
            'baidu' => 0,
            'wechat' => 0,
            'alipay' => 0,
            'total' => 0,
            'today' => 0,
        ];
        $current_id_arr = model('custom/custom')->where(['parent_current_id' => $this->custom->current_id,'status' => 1])->column('current_id');
        $current_id_arr = array_unique($current_id_arr);
        if ($current_id_arr) {
            $custom_sites = model('site/Site')->where('custom_id' , 'in' ,$current_id_arr)->order('create_time DESC')->select();
            if (count($custom_sites) > 0){
                foreach ($custom_sites as $k => $v) {
                    $type = explode(',',$v['type']);
                    if (in_array('baidu',$type)) {
                        $site_count['baidu'] += 1;
                    } else if (in_array('wechat',$type)) {
                        $site_count['wechat'] += 1;
                    } else if (in_array('alipay',$type)) {
                        $site_count['alipay'] += 1;
                    }
                    if (strtotime($v['create_time']) >= strtotime(date('Y-m-d'))) {
                        $site_count['today'] += 1;
                    }
                }
                $site_count['total'] = count($custom_sites);
            }
        }
        $this->assign('site_count',$site_count);

        // 我的站点
        $mySites = [];
        $where = [];
        $where[] = ['custom_id','=',$this->custom->current_id];
        $where[] = ['status','=',1];
        $where[] = ['deadline','between',[time() ,time()+86400*7]];
        $mySites['renew'] = (int) model('site/Site')->where($where)->count();

        $where = [];
        $where[] = ['custom_id','=',$this->custom->current_id];
        $where[] = ['status','=',1];
        $where[] = ['deadline','<',time()];
        $where[] = ['deadline','neq',0];
        $mySites['invalid'] = (int) model('site/Site')->where($where)->count();
        $this->assign('mySites',$mySites);

        // 公告
        $notices = model('notice/Notice')->field('id,title,content,create_time')->where('status',1)->order('create_time desc')->limit(4)->select();
        $this->assign('notices',$notices);

    	return $this->fetch('home');
    }

    public function ajax_home() {
        $day_num = 15;
        $dates = [];
        for ($i = 1; $i <= $day_num; $i++) {
            $dates[] = date('m-d', strtotime('-'.$i.' days'));
        }
        sort($dates);

        $res = [
            'dates' => $dates,
            'recharge' => [],
            'deduction' => [],
            'user' => [],
        ];

        // 充值消费记录
        $current_id_arr = model('custom/custom')->where(['parent_current_id' => $this->custom->current_id,'status' => 1])->column('current_id');
        $current_id_arr[] = $this->custom->current_id;
        $current_id_arr = array_unique($current_id_arr);
        $current_id_arr = array_values($current_id_arr);
        $uname = '';
        for($i=0;$i<count($current_id_arr);$i++){
            $uname .= "'".$current_id_arr[$i]."',";
        }
        $uname = rtrim($uname, ",");

        $sql = "SELECT FROM_UNIXTIME(create_time, '%Y-%m-%d') AS time, count(*) AS num,type";
        $sql .= " FROM custom_fee";
        $sql .= " WHERE current_id in (".$uname.")";
        $sql .= " AND DATE_SUB(CURDATE(), INTERVAL ".count($dates)." DAY) <= DATE(FROM_UNIXTIME(create_time, '%Y-%m-%d'))";
        $sql .= " GROUP BY time,type";
        $first = \think\Db::query($sql);
        $fees_arr = [];
        if ($first) {
            foreach ($first as $v) {
                $date_arr = explode('-',$v['time']);
                array_shift($date_arr);
                $date = implode('-', $date_arr);

                if ($v['type'] == 'recharge') {
                    $fees_arr[$date]['recharge'] = $v['num'];
                } else {
                    $fees_arr[$date]['deduction'] = $v['num'];
                }
            }
        }

        // 新增用户走势图
        $sql = "SELECT FROM_UNIXTIME(create_time, '%m-%d') AS time, count(*) AS num";
        $sql .= " FROM custom";
        $sql .= " WHERE parent_current_id = '".$this->custom->current_id."' AND DATE_SUB(CURDATE(), INTERVAL ".count($dates)." DAY) <= DATE(FROM_UNIXTIME(create_time, '%Y-%m-%d'))";
        $sql .= " GROUP BY time";
        $add_users = \think\Db::query($sql);
        $add_users_arr = [];
        if ($add_users) {
            foreach ($add_users as $v) {
                $add_users_arr[$v['time']] = $v['num'];
            }
        }

        $return_first = $return_second = $users = [];
        foreach ($dates as $k => $v) {
            $return_first[$k] = 0;
            $return_second[$k] = 0;
            if (isset($fees_arr[$v])) {
                if (isset($fees_arr[$v]['recharge'])) {
                    $return_first[$k] = $fees_arr[$v]['recharge'];
                }
                if (isset($fees_arr[$v]['deduction'])) {
                    $return_second[$k] = $fees_arr[$v]['deduction'];
                }
            }

            $users[$k] = 0;
            if (isset($add_users_arr[$v])) {
                $users[$k] = $add_users_arr[$v];
            }
        }
        $res['recharge'] = $return_first;
        $res['deduction'] = $return_second;
        $res['user'] = $users;
        

        return Json::create(['code' => 1,'msg' => '请求成功', 'info' => $res]);
    }
}